package com.train.daily.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.daily.dto.station.DailyTrainStationQueryDTO;
import com.train.daily.resp.station.DailyTrainStationPageResp;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.daily.resp.station.DailyTrainStationPageQueryResp;
import com.train.mybatis.base.MyBaseMapper;
import com.train.daily.dto.station.DailyTrainStationAddOrEditDTO;
import com.train.daily.dto.station.DailyTrainStationDTO;
import com.train.daily.dto.station.DailyTrainStationPageDTO;
import com.train.daily.po.DailyTrainStationPO;

import java.time.LocalDate;
import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【dailyTrainStation(每日车站)】的数据库操作Mapper
 * @createDate 2025-03-01 14:35:49
 * @Entity generator.domain.DailyTrainStation
 */
public interface DailyTrainStationMapper extends MyBaseMapper<DailyTrainStationPO> {

    default PageResp<DailyTrainStationPageResp> pageList(DailyTrainStationPageDTO dailyTrainStationPageDTO) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(dailyTrainStationPageDTO.getCode()), DailyTrainStationPO::getTrainCode, dailyTrainStationPageDTO.getCode())
                .eq(ObjectUtil.isNotEmpty(dailyTrainStationPageDTO.getDate()), DailyTrainStationPO::getDate, dailyTrainStationPageDTO.getDate());
        PageResp<DailyTrainStationPO> dailyTrainStationPOPageResp = this.selectPage(
                dailyTrainStationPageDTO,
                queryWrapper, new PageSorted(DailyTrainStationPO::getUpdateTime), new PageSorted(DailyTrainStationPO::getCreateTime));
        List<DailyTrainStationPageResp> dailyTrainStationPageRespList = BeanUtil.copyToList(dailyTrainStationPOPageResp.getData(), DailyTrainStationPageResp.class);
        return new PageResp<>(dailyTrainStationPOPageResp.getTotal(), dailyTrainStationPageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainStationPO::getId, id);
        return this.exists(queryWrapper);
    }

    default boolean exists(DailyTrainStationAddOrEditDTO dailyTrainStationAddOrEditDTO) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainStationPO::getTrainCode, dailyTrainStationAddOrEditDTO.getTrainCode())
                .eq(DailyTrainStationPO::getDate, dailyTrainStationAddOrEditDTO.getDate())
                .and(wrapper -> wrapper.eq(DailyTrainStationPO::getIndex, dailyTrainStationAddOrEditDTO.getIndex())
                        .or().eq(DailyTrainStationPO::getName, dailyTrainStationAddOrEditDTO.getName()));
        return this.exists(queryWrapper);
    }

    default List<DailyTrainStationDTO> getTrainStationList(DailyTrainStationAddOrEditDTO dailyTrainStationAddOrEditDTO) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DailyTrainStationPO::getId, DailyTrainStationPO::getIndex, DailyTrainStationPO::getName)
                .eq(DailyTrainStationPO::getTrainCode, dailyTrainStationAddOrEditDTO.getTrainCode())
                .eq(DailyTrainStationPO::getDate, dailyTrainStationAddOrEditDTO.getDate());
        List<DailyTrainStationPO> trainStationPOS = this.selectList(queryWrapper);
        return BeanUtil.copyToList(trainStationPOS, DailyTrainStationDTO.class);
    }

    default boolean deleteTrainStations(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainStationPO::getDate, date)
                .in(DailyTrainStationPO::getTrainCode, trainCodes);
        return this.delete(queryWrapper) > 0;
    }

    default boolean existsTrainStation(LocalDate date, List<String> trainCodes) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainStationPO::getDate, date)
                .in(DailyTrainStationPO::getTrainCode, trainCodes)
                .last("limit 1");
        return this.exists(queryWrapper);
    }

    default Long countDailyTrainStation(LocalDate date, String trainCode) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DailyTrainStationPO::getDate, date)
                .eq(DailyTrainStationPO::getTrainCode, trainCode);
        return this.selectCount(queryWrapper);
    }

    default PageResp<DailyTrainStationPageQueryResp> pageList(DailyTrainStationQueryDTO dto) {
        LambdaQueryWrapper<DailyTrainStationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(DailyTrainStationPO::getIndex, DailyTrainStationPO::getName, DailyTrainStationPO::getInTime,
                        DailyTrainStationPO::getOutTime, DailyTrainStationPO::getStopTime)
                .eq(DailyTrainStationPO::getTrainCode, dto.getTrainCode())
                .eq(DailyTrainStationPO::getDate, dto.getDate());
        PageResp<DailyTrainStationPO> pageResp = this.selectPage(dto, queryWrapper);
        List<DailyTrainStationPageQueryResp> queryRespList = BeanUtil.copyToList(pageResp.getData(), DailyTrainStationPageQueryResp.class);
        return new PageResp<>(pageResp.getTotal(), queryRespList);
    }
}